package Logic;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Database.AuditLogDB;
import Model.Committee;
import Model.CommitteeRole;
import Model.Staff;

/**
 * Servlet implementation class CommitteeEdit_BLL
 */
@WebServlet("/CommitteeEdit_BLL")
public class CommitteeEdit_BLL extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CommitteeEdit_BLL() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String title = request.getParameter("title");
		String roleResponsibility = request.getParameter("r&r");
		String purpose = request.getParameter("purpose");
		String frequencyofmeeting = request.getParameter("fom");
		String committeeId = request.getParameter("id");
		String date = request.getParameter("effectivedate");

		String oTitle = request.getParameter("originalTitle");
		String oPurpose = request.getParameter("originalPurpose");
		String oRoleResponsibility = request.getParameter("originalR&R");
		String oFrequencyOfMeeting = request.getParameter("originalFom");
		String oDate = request.getParameter("originalDate");

		if (request.getParameter("submit").equals("Save & Continue")) {

			System.out.println("Modifying committee");
			

			
			
			
			if (title.equals(oTitle)
					&& roleResponsibility.equals(oRoleResponsibility)
					&& purpose.equals(oPurpose)
					&& frequencyofmeeting.equals(oFrequencyOfMeeting)
					&& date.equals(oDate)) {

				ArrayList<CommitteeRole> commList = Database.CommitteeRoleDB
						.getCommitteeRole(committeeId);
				int listSize = commList.size();
				for (int i = 0; i < listSize; i++) {
					request.setAttribute("roleName" + i, commList.get(i)
							.getName());
					request.setAttribute("staffName" + i, commList.get(i)
							.getStaffName());
					

					if(  !commList.get(i).getStaffName().equals("") )
					{
						
						//seperate staff  by , into an array
						String[] stafflist2 = commList.get(i).getStaffName().split(", ");

						ArrayList<String> staffnamelist = new ArrayList<String>();
						//send staff login id and return name and store in array
						for (int x = 0; x < stafflist2.length;  x++) {
							Staff staffname1 ;
							staffname1 = Database.CredentialDB.getStaffName(stafflist2[x]);
							staffnamelist.add(staffname1.getName());
							
						}
						request.setAttribute("staffnamelist" + i,staffnamelist);
						
					}
					
	
				}
				request.setAttribute("commListRow", listSize);
				request.getRequestDispatcher(
						"committee_editcomm.jsp?action=e&id=" + committeeId)
						.forward(request, response);
			} else if (title == "" | roleResponsibility == "" | purpose == "" | date == "") {
				response.sendRedirect("committee_modifycomm.jsp?action=e&error=1&id="
						+ committeeId);
			} else if (!title.equals(oTitle)) {
				if (Database.CommitteeDB.titleExists(title)) {
					response.sendRedirect("committee_modifycomm.jsp?action=e&error=2&id="
							+ committeeId);
				} else {
					Committee com = new Committee();
					com.setId(committeeId);
					com.setTitle(title);
					com.setPurpose(purpose);
					com.setRolesResponsibility(roleResponsibility);
					com.setFrequencyOfMeeting(frequencyofmeeting);
					com.setDate(date);

					Database.CommitteeDB.updateCommittee(com);
					AuditLogDB.insertLog("update committee", (String) request
							.getSession().getAttribute("name"));
					
					ArrayList<CommitteeRole> commList = Database.CommitteeRoleDB
							.getCommitteeRole(committeeId);
					int listSize = commList.size();
					for (int i = 0; i < listSize; i++) {
						request.setAttribute("roleName" + i, commList.get(i)
								.getName());
						request.setAttribute("staffName" + i, commList.get(i)
								.getStaffName());
						
						if(  !commList.get(i).getStaffName().equals("") )
						{
							
							//seperate staff  by , into an array
							String[] stafflist2 = commList.get(i).getStaffName().split(", ");

							ArrayList<String> staffnamelist = new ArrayList<String>();
							//send staff login id and return name and store in array
							for (int x = 0; x < stafflist2.length;  x++) {
								Staff staffname1 ;
								staffname1 = Database.CredentialDB.getStaffName(stafflist2[x]);
								staffnamelist.add(staffname1.getName());
								
							}
							request.setAttribute("staffnamelist" + i,staffnamelist);
							
						}
						
				
					}
					request.setAttribute("commListRow", listSize);
					request.getRequestDispatcher(
							"committee_editcomm.jsp?action=e&id=" + committeeId)
							.forward(request, response);
				}
			} else {
				Committee com = new Committee();
				com.setId(committeeId);
				com.setTitle(title);
				com.setPurpose(purpose);
				com.setRolesResponsibility(roleResponsibility);
				com.setFrequencyOfMeeting(frequencyofmeeting);
				com.setDate(date);

				Database.CommitteeDB.updateCommittee(com);
				AuditLogDB.insertLog("update committee", (String) request
						.getSession().getAttribute("name"));

				ArrayList<CommitteeRole> commList = Database.CommitteeRoleDB
						.getCommitteeRole(committeeId);
				int listSize = commList.size();
				for (int i = 0; i < listSize; i++) {
					request.setAttribute("roleName" + i, commList.get(i)
							.getName());
					request.setAttribute("staffName" + i, commList.get(i)
							.getStaffName());
					
					
					if(  !commList.get(i).getStaffName().equals("") )
					{
						
						//seperate staff  by , into an array
						String[] stafflist2 = commList.get(i).getStaffName().split(", ");

						ArrayList<String> staffnamelist = new ArrayList<String>();
						//send staff login id and return name and store in array
						for (int x = 0; x < stafflist2.length;  x++) {
							Staff staffname1 ;
							staffname1 = Database.CredentialDB.getStaffName(stafflist2[x]);
							staffnamelist.add(staffname1.getName());
							
						}
						request.setAttribute("staffnamelist",staffnamelist);
						
					}
					

				}
				request.setAttribute("commListRow", listSize);
				request.getRequestDispatcher(
						"committee_editcomm.jsp?action=e&id=" + committeeId)
						.forward(request, response);
			}
		} else {
			if (request.getParameter("search").equals("true")) {
				request.setAttribute("id",  request.getParameter("id"));
				request.getRequestDispatcher("committee_searchresult.jsp")
						.forward(request, response);
			} else {
				response.sendRedirect("committee_viewcomm.jsp");
			}
		}
			
	}
}
